export function arrayBufferToBase64(buffer: ArrayBuffer) {
  var binary = ''
  var bytes = new Uint8Array(buffer)
  var len = bytes.byteLength
  for (var i = 0; i < len; i++) {
    binary += String.fromCharCode(bytes[i])
  }
  return window.btoa(binary)
}
// 文件分片上传 chunkSize默认值为1MB
export function sliceAndUploadFile(file: File, onUpload: Function, chunkSize = 1024 * 1024) {
  const totalChunks = Math.ceil(file.size / chunkSize)
  for (let i = 0; i < totalChunks; i++) {
    let start = i * chunkSize
    let end = start + chunkSize
    if (end > file.size) {
      end = file.size
    }
    let chunk = file.slice(start, end)
    let reader = new FileReader()
    reader.onload = function (e) {
      let chunkData = e?.target?.result
      // 这里可以将chunkData发送到服务器
      onUpload(chunkData, i === totalChunks - 1)
    }
    reader.readAsArrayBuffer(chunk)
  }
}
